<?php
namespace app\api\model;

use app\BaseModel;
use think\Db;

class MemberModel extends BaseModel {

    	
	public function ddd(){
		echo "fdf";
	}
	
	/**
	 * 检测账号和密码
	 */
	public function loginByAccountPhoneWithPass($accountOrPhone,$password){
		$result = Db::query(" select * from t_member m where (m.account=? and m.password=?) or (m.phone=? and m.password=?)  ",[$accountOrPhone,$password,$accountOrPhone,$password]);
		if(empty($result)){
			return $result;
		}
		return $result[0];
	}
	
	
	//更新token
	public function updateUserToken($userToken){
		$memberId 	= $userToken["memberId"];
		
		$findUToken = Db::name('t_userToken')->where('memberId', $memberId)->find();
		if(empty($findUToken)){//添加
			Db::name('t_user_token')->insert($userToken);
		}else{//更新
			Db::name("t_user_token")->where("id",$findUToken["id"])->update($userToken);
		}
	}
	
		//创建用户
	function createMember($instance){
			$phone 		= $instance["phone"];
			$password 	= $instance["password"];
			$checkCode 	= $instance["checkCode"];
			$openid	 	= $instance["openid"];
			
			$nickName	= $instance["nickName"];
			$realName	= $instance["realName"];
			$sex	 		= $instance["sex"];
			$avatar	 	= $instance["avatar"];
			$province	 = $instance["province"];
			$city	 	= $instance["city"];
			
			//检测手机号
			$member = Db::name('t_member')->where("phone",$phone)->find();
			
			if(!empty($member)){
				return array("success"=>FALSE,"error"=>"该手机号码已经被注册了");
			}
			
			
			
			//检查openid
			if(!empty($openid)){
				$member = Db::name('t_member')->where("openid",$openid)->find();
				if(!empty($member)){
					return array("success"=>FALSE,"error"=>"该微信已经被注册了");
				}
			}
			
			//检查验证码是否正确
			$usercheckcode = Db::name('t_checkcode')->where("phone",$phone)->find();
			
			if(empty($usercheckcode) || $usercheckcode["checkCode"]!=$checkCode){
				return array("success"=>FALSE,"error"=>"验证码不正确");
			}else{
				$t = $usercheckcode["validTime"];
				if($t<(time())){
					return array("success"=>FALSE,"error"=>"验证码已经失效");
				}
			}
			
			//创建用户
			$memberId = Db::name("t_member")->insertGetId(array("openid"=>$openid,"phone"=>$phone,"password"=>$password,"createTime"=>eeGetDateTime()
			,"nickName"=>$nickName,"realName"=>$realName,"sex"=>$sex,"avatar"=>$avatar,"province"=>$province,"city"=>$city));
			
			if(!empty($memberId)){
				
				$memberInstance = Db::name("t_member")->where("id",$memberId)->find();
				
				return array("success"=>TRUE,"instance"=>$memberInstance);
			}else{
				return array("success"=>FALSE,"error"=>"创建用户的时候失败");
			}	
		}
		
		
		//得到粉丝
		function getFollws($memberId){
			$list = Db::query("select f.id as followId, m.id as memberId,m.account,m.realName,m.sex,m.avatar from t_follow f left join t_member m on f.followId=m.id where f.memberId=?",[$memberId]);
			return $list;
		}
//		
//		//删除粉丝
//		function deleteFollow($memberId,$followId){
//			//$this->deleteBy("t_follow", array("memberId"=>$memberId,"followId"=>$followId));
//			Db::name("t_follow")->where("memberId",$memberId)->where("followId",$followId)->delete();
//		}
}